﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.Web;
using System.Data.SqlClient;
using System.IO;


namespace comix
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    public class rp : IHttpHandler
    {
        const int Synchronize = 200;
        const int PreviewImages = 201;

        private HttpContext _context;        

        public void ProcessRequest(HttpContext context)
        {
            _context = context;
            _context.Response.ContentType = "text/xml";
            _context.Response.ContentEncoding = System.Text.Encoding.UTF8;
            int opcode = Convert.ToInt32(context.Request.QueryString["op"]);           
            switch (opcode)
            {
                case Synchronize:
                    DoSynchronize();
                    break;
                case PreviewImages:
                    DoPreviewImages();
                    break;
            }
        }

        public void DoPreviewImages()
        {
            XmlDocument doc = RpHelper.PreparePlistXML();
            XmlElement a = doc.CreateElement("array");
            doc.DocumentElement.AppendChild(a);
            string dirpath = _context.Request.MapPath("previews/" + _context.Request.QueryString["p1"]);
            if (Directory.Exists(dirpath))
            {
                DirectoryInfo di = new DirectoryInfo(dirpath);
                FileInfo [] files =  di.GetFiles("*.jpg");
                foreach (FileInfo fi in files)
                {
                    RpHelper.AddStringValue(fi.Name, doc, a);
                }
            }
            _context.Response.Write(doc.OuterXml);
            _context.Response.End();
        }

        public void DoSynchronize()
        {
            XmlDocument doc = RpHelper.PreparePlistXML();
            XmlElement dict = doc.CreateElement("dict");
            doc.DocumentElement.AppendChild(dict);
            RpHelper.AddDictionaryKey("time", doc, dict);
            DateTime dt = DateTime.Now;
            RpHelper.AddStringValue(dt.ToString("s"),doc,dict);
            
            SqlCommand com = new SqlCommand();
            com.Parameters.Add(new SqlParameter("@updatedate", Convert.ToDateTime(_context.Request.QueryString["p1"])));

            com.CommandText = "SELECT * FROM PARENTAL WHERE par_updatedate>@updatedate";
            RpHelper.AddPlistArrayForKey("parental", doc, dict, RpHelper.Sql2PlistArray(com, doc));

            com.CommandText = "SELECT * FROM PUBLISHERS WHERE pub_updatedate>@updatedate";
            RpHelper.AddPlistArrayForKey("publishers", doc, dict, RpHelper.Sql2PlistArray(com, doc));

            com.CommandText = "SELECT * FROM CATEGORIES WHERE cat_updatedate>@updatedate";
            RpHelper.AddPlistArrayForKey("categories", doc, dict, RpHelper.Sql2PlistArray(com, doc));

            com.CommandText = "SELECT * FROM AUTHORS WHERE aut_updatedate>@updatedate";
            RpHelper.AddPlistArrayForKey("authors", doc, dict, RpHelper.Sql2PlistArray(com, doc));

            com.CommandText = "SELECT * FROM SERIES WHERE ser_updatedate>@updatedate";
            RpHelper.AddPlistArrayForKey("series", doc, dict, RpHelper.Sql2PlistArray(com, doc));

            com.CommandText = "SELECT * FROM BOOKS WHERE bok_updatedate>@updatedate";
            RpHelper.AddPlistArrayForKey("books", doc, dict, RpHelper.Sql2PlistArray(com, doc));

            com.CommandText = "SELECT * FROM AUTHORS2SERIES WHERE a2s_updatedate>@updatedate";
            RpHelper.AddPlistArrayForKey("authorsofseries", doc, dict, RpHelper.Sql2PlistArray(com, doc));

            com.CommandText = "SELECT * FROM BOOKS2AUTHORS WHERE a2b_updatedate>@updatedate";
            RpHelper.AddPlistArrayForKey("authorsofbooks", doc, dict, RpHelper.Sql2PlistArray(com, doc));

            com.Parameters.Clear();
            com.CommandText = "SELECT bok_id, bok_rating, bok_sales FROM BOOKS";
            RpHelper.AddPlistArrayForKey("bookstatistics", doc, dict, RpHelper.Sql2PlistArray(com, doc));

            _context.Response.Write(doc.OuterXml);
            _context.Response.End();
        }



        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
